﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CardGamesLibrary.Cards;

namespace CardGamesLibrary.UI
{
    public partial class FrenchDeckCardControl : UserControl
    {
        private FrenchDeckCard _card;

        public FrenchDeckCardControl()
        {
            InitializeComponent();
            QueryContinueDrag += new QueryContinueDragEventHandler(FrenchDeckCardControl_QueryContinueDrag);
        }

       

        public FrenchDeckCardControl(FrenchDeckCard card) : this()
        {
            SetCard(card);
        }

        private void FrenchDeckCardControl_Load(object sender, EventArgs e)
        {
            
        }

        public void SetCard(FrenchDeckCard card)
        {
            _card = card;

            if (InvokeRequired)
            {
                Invoke((MethodInvoker)delegate() { Refresh(); });
            }
            else {
                Refresh();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
           
            base.OnPaint(e);
           
            if (_card != null && !_card.Reversed)
            {
               
                
                var carImgName = "_" + _card.ToString();
                

               var bmp = ClassicCards.ResourceManager.GetObject(carImgName) as Bitmap;
               if (bmp != null)
               {
                   e.Graphics.DrawImage(bmp, 0, 0, e.ClipRectangle.Width,((int)( e.ClipRectangle.Width * ((decimal)ClassicCards.b1fv.Height / ClassicCards.b1fv.Width))));
               }
               //var brush = _card.Color == SuitColor.Red ? Brushes.Red : Brushes.Black;
               //e.Graphics.DrawString(_card.ToString(), Font, brush, 10, 10);
            }
            if (_card != null && _card.Reversed)
            {

                e.Graphics.DrawImage(ClassicCards.b1fv, 0, 0, e.ClipRectangle.Width, 130);
               
            }
            if (_card == null && !DesignMode)
            {
                e.Graphics.DrawRectangle(Pens.LightGray, 0, 0, e.ClipRectangle.Width,e.ClipRectangle.Height);

            }
        }

        protected override void OnDragDrop(DragEventArgs drgevent)
        {
            base.OnDragDrop(drgevent);
        }

        bool selected;
        bool moving;
        
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (!selected)
                selected = true;

            //DoDragDrop(_card, DragDropEffects.All);
            base.OnMouseDown(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (selected)
            {
                selected = false;
            }
            if (moving)
            {
                moving = false;
            }
            base.OnMouseUp(e);
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (selected && ! moving)
            {
                moving = true;
                DoDragDrop(_card, DragDropEffects.All);
            }
          base.OnMouseMove(e);
        }

        void FrenchDeckCardControl_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
        {
           
           // e.Action = DragAction.Continue;
            
        }
    }
}
